package com.example.httpclient.Crawler;

import android.os.Handler;
import android.os.Message;

import com.example.httpclient.uilts.StringSplit;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class ImageUrl implements Runnable {
//    private static Set<String> visitedUrl=new HashSet<>();
    private ArrayList<String> UnVisitUrl;
    private String indexUrl;
    private ArrayList<String> DataList;
    private getImageUrlHandler getImageUrl;
    private static ArrayList<String> finallyUrl=new ArrayList<>();
    private Handler mHandler;

    public ImageUrl(ArrayList<String> VisitUrls, String indexUrl, ArrayList<String> DataList, getImageUrlHandler getImageUrlHandler, Handler mHandler) {
        this.UnVisitUrl = VisitUrls;
        this.indexUrl = indexUrl;
        this.DataList=DataList;
        this.getImageUrl= getImageUrlHandler;
        this.mHandler=mHandler;
    }
    
    @Override
    public void run() {
        mHandler.sendEmptyMessage(0x221);
        if(UnVisitUrl.size()>0){
            for (int i=0;i<UnVisitUrl.size();i++) {
                String urlString=UnVisitUrl.get(i);
                try {
                    if (!urlString.contains(indexUrl)&&!urlString.contains("https://")&&!urlString.contains("http://")) {
                        urlString= "https://"+indexUrl + urlString;
                    }
                    URL urL = new URL(urlString);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) urL.openConnection();
                    if (httpURLConnection.getResponseCode() == 200) {
                        Message message=new Message();
                        message.what=0x00;
                        message.obj="网址： "+urlString+"加载成功";
                        mHandler.sendMessage(message);
                        Document document = Jsoup.parse(urL, 5 * 1000);
                        Elements ImgElements = document.getElementsByTag("img");
                        for (Element ImgElement : ImgElements) {
                            String imgUrl = ImgElement.attr(DataList.get(0));
                            if(!imgUrl.contains(indexUrl)&&!imgUrl.contains("https://")&&!imgUrl.contains("http://")){
                                imgUrl="https://"+indexUrl+imgUrl;
                            }
                            if(DataList.get(1).length()!=0&&DataList.get(2).length()!=0){
                                imgUrl= StringSplit.changeUrl(imgUrl,DataList.get(1),DataList.get(2));
                            }
                            finallyUrl.add(imgUrl);
                        }
                    }
                } catch (IOException e) {
                    mHandler.sendEmptyMessage(0x222);
                }finally {
                    getImageUrl.getUrlHandler(finallyUrl);
                }
            }
        } else {//if(UnVisitUrl.size()>0)
            Message message=new Message();
            message.what=0x00;
            message.obj="网址获取失败，请稍后重试";
            mHandler.sendMessage(message);
            mHandler.sendEmptyMessage(0x222);
        }
    }
}
